import {
  AsyncStorage,
} from 'react-native';

export default class readDao{
  getData(url){
      return new Promise((resolve,reject)=>{
          AsyncStorage.getItem(url,(error,result)=>{
              if(error){
                  reject(error);
                  return;
              }
              if (!result){
                  resolve('');
              }else {
                // 假如存在
                let nTime = new Date().getTime();
                let oTime = JSON.parse(result).time
                if(nTime > oTime + (2*60*60*1000)) {
                  resolve('')
                } else {
                  try {
                      resolve(JSON.parse(result).data);
                  } catch (e) {
                      reject(error);
                  }
                }
              }
          });
      });
  }
  saveData(url, objectData){
      let obj = {
        data: objectData,
        time: new Date().getTime()
      }
      AsyncStorage.setItem(url,JSON.stringify(obj),(error,result)=>{

      });
  }
  // data, time
}